Use Oxygen conditions to show elements to users with ACF field checked

Written for the author of this Facebook post. Jonathan wanted to conditionally show an element using the Oxygen conditions feature, if the user's ACF field 'webinar' had a certain value checked.

There were multiple values, with more potentially being added in future, so I wrote a function which took the value to be checked as an argument, and looked in the user's checkbox field for that value. If it's there, we return 1. If not, we return 0.

<?php
function sw_user_field_condition_check($compare) { // compare = the value you're checking

  // check if user is logged in, if not, do nothing more
  if(!is_user_logged_in()) {
    return;
  }

  // get the users ID and save to variable
  $user_id = get_current_user_id();

  // get acf field
  $checkbox_field = get_field('checkbox1', 'user_'. $user_id ); // this should be set to return values, in the ACF field settings

  // check if compare value is in the array, and if so, return 1
  if( $checkbox_field && in_array($compare, $checkbox_field) ) {
    return '1';
  } else {
    return '0';
  }

}
?>

Now you just need to make an Oxygen condition that uses this function. For example, when trying to show an element when 'checkbox1' is checked:

  1. Select the element you want to show when 'checkbox1' is selected and create a condition
  2. Select from the first dropdown: Dynamic Data > PHP Function Return Value
  3. Function name = sw_user_field_condition_check
  4. Function arguments = checkbox1
  5. Click insert. Then type into custom value: 1
  6. Save template. Users who have icar ticked should now be able to see the element, but not otherwise.

Done! Hope that was helpful. If you want any custom WordPress functions or scripts built, just send me an email to [email protected].

crossmenu linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram